//
//  YDBuySection2Cell.m
//  PeiZiWangBei
//
//  Created by 巨彦新 on 2018/3/2.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDBuySection2Cell.h"

@interface YDBuySection2Cell()

/**
 股票名称
 */
@property (nonatomic, strong)UILabel *stockName;

/**
 提示图片
 */
@property (nonatomic, strong)UIImageView *questionImage;

/**
 按钮数组
 */
@property (nonatomic, strong)NSMutableArray *btnArr;

@end
@implementation YDBuySection2Cell



-(void)setCycleList:(NSMutableArray *)cycleList
{
    _cycleList = cycleList;
    [self.btnArr removeAllObjects];
    
    [self addChooseBtn];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self stockName];
        [self questionImage];
    
    }
    return self;
}


- (void)tapClick:(UITapGestureRecognizer *)tap
{
    if (self.clickBack) {
        self.clickBack(@"问号", 0);
    }
}


- (NSMutableArray *)btnArr
{
    if (!_btnArr) {
        _btnArr = [NSMutableArray new];
    }
    return _btnArr;
}
- (UILabel *)stockName
{
    if (!_stockName) {
        _stockName = [UILabel new];
        _stockName.text = @"策略周期";
        _stockName.textColor = [UIColor colorWithHexString:blueyGrey];
        _stockName.font = [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
        [self.contentView addSubview:_stockName];
        [_stockName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(rate375(16));
            make.top.equalTo(self.contentView).offset(isPhoneX? rateX812(15) :rate750(15));
        }];
        
    }
    return _stockName;
}
-(UIImageView *)questionImage
{
    if (_questionImage == nil) {
        UIImageView *questtionImage = [[UIImageView alloc] init];
        [self.contentView addSubview:questtionImage];
        questtionImage.userInteractionEnabled = YES;
        [questtionImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.stockName.mas_right).offset(rate375(6));
            make.centerY.equalTo(self.stockName.mas_centerY);
        }];
        questtionImage.image = [UIImage imageNamed:@"zdqq-zhujie"];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [questtionImage addGestureRecognizer:tap];
        
        _questionImage = questtionImage;
    }
    return _questionImage;
}


- (void)addChooseBtn
{
    //整个按钮区域的宽度
    CGFloat buttonsViewW =  LKScreenW - rate375(40);
    //按钮间的横向间距
    CGFloat marginH = rate375(14);
    //按钮间的竖向间距
    CGFloat marginV = isPhoneX ? rateX812(13) : rate750(13);
    //按钮的高度
    CGFloat buttonH = isPhoneX ? rateX812(24) : rate750(24);
    
    
    CGFloat y = isPhoneX ? rateX812(50) : rate750(50);

    for (int i = 0; i < self.cycleList.count; i ++) {
        
        //每行显示的个数
        NSInteger cows = 4;
        //按钮的宽度
        CGFloat buttonW = (buttonsViewW - (cows - 1)*marginH) / cows;
        
        //确定第几列第几行
        NSInteger whichCow = i / cows;
        NSInteger whichCol = i % cows;

        //确定按钮的x  y
        
        CGFloat buttonX = whichCol * (marginH + buttonW );
        CGFloat buttonY = whichCow * (marginV + buttonH );
        
        SYNoHeightedButton *button = [self createBtnWithFundModel:[self.cycleList objectAtIndex:i]];
        if (i == 0) {
            button.selected = YES;
            button.layer.borderColor = [UIColor clearColor].CGColor;
        }
        button.frame = CGRectMake(rate375(20) + buttonX, buttonY + y , buttonW, buttonH);
        
        [self.contentView addSubview:button];
        
        button.tag = i + 100;
        
       [self.btnArr addObject:button];
        
    }
}

- (SYNoHeightedButton *)createBtnWithFundModel:(NSString *)name
{
    SYNoHeightedButton *button =  [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize:font12ScreenWRate * LKScreenW];
    button.titleLabel.adjustsFontSizeToFitWidth = YES;
    button.backgroundColor = [UIColor clearColor];
    // NSString *wan =   [model.value substringWithRange:NSMakeRange(0, model.value.length - 4)];
    [button setTitle:[NSString stringWithFormat:@"%@", name] forState:UIControlStateNormal];
    
    //按钮颜色
    [button setTitleColor:[UIColor colorWithHexString:blueyGrey] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    //背景
    [button setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:fadedOrange]] forState:UIControlStateSelected];
    //边框
    button.layer.cornerRadius = 3.0f;
    button.layer.masksToBounds = YES;
    button.layer.borderWidth = 1.0f;
    button.layer.borderColor = [UIColor colorWithHexString:blueyGrey].CGColor;
    [button addTarget:self action:@selector(fundBtnDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}


- (void)fundBtnDidSelected:(UIButton *)sender
{
    [self.btnArr enumerateObjectsUsingBlock:^(UIButton * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = NO;
        obj.layer.borderColor = [UIColor colorWithHexString:blueyGrey].CGColor;
    }];
    sender.selected  =YES;
    
    sender.layer.borderColor = [UIColor clearColor].CGColor;
    if (self.clickBack) {
        self.clickBack(@"", sender.tag - 100);
    }
}
@end
